package it.unibo.mumu;

import java.util.ArrayList;

import it.unibo.mumu.R;
import it.unibo.mumu.entity.Planet;
import it.unibo.mumu.entity.UniverseSelected;
import it.unibo.mumu.util.SingletonSupport;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
//import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class PlanetDetailActivity extends Activity {

	private ImageButton multimediaButton;
	private ImageButton imageButton;
	private ImageButton warperButton;
	
	private int universeId;
	private int planetId;
	private ArrayList<UniverseSelected> universeList;
	private Planet planet;
	
//	private int realIDuniverse, realIDplanet;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_planet_detail);
		
//		Intent intent = getIntent();
		
		universeId = SingletonSupport.getInstance().getUniverseID(); // id da xml
		planetId = SingletonSupport.getInstance().getPlanetID();// id da xml
//		Log.i("tmp", String.valueOf(universeId));
//		Log.i("tmp", String.valueOf(planetId));
		TextView textView = (TextView)findViewById(R.id.TextView01);
		
		universeList = SingletonSupport.getInstance().getUniverses();

		SingletonSupport.getInstance().setRealUniverseID(universeId);
		SingletonSupport.getInstance().setRealPlanetID(planetId);
		
//		Log.i("tmp", "universe: "+SingletonSupport.getInstance().getRealUniverseID());
//		Log.i("tmp", "planet: "+SingletonSupport.getInstance().getRealPlanetID());
		
		if (GalaxyActivity.DEBUG){
			textView.setText("DEBUG\nMODE");
		} else {
			planet = universeList.get(SingletonSupport.getInstance().getRealUniverseID()).getPlanets().get(SingletonSupport.getInstance().getRealPlanetID());
			textView.setText(planet.getTitle1()+"\n"+planet.getTitle2()+"\n"+planet.getTitle3());
		}

//		Toast.makeText(getApplicationContext(), "Universo "+universeId+", pianeta "+planetId,Toast.LENGTH_LONG).show();
		
		multimediaButton = (ImageButton) findViewById(R.id.rightButton);
		multimediaButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				
				if(SingletonSupport.getInstance().isHappiness()){
					
					// parte HAPPY
					// determina se ho audio o video
					if(!planet.getMultimediaHappy().getAudio().getAudioTitle().equals("")){
						// contenuto audio
//						Log.i("MULTIMEDIA","audio-> "+planet.getMultimediaHappy().getAudio().getAudioTitle());
						
						Intent toMultimedia = new Intent(PlanetDetailActivity.this, AudioPlayerActivity.class);
						System.gc();
			        	finish();
			        	startActivity(toMultimedia);
						
					}else{
						// contenuto video
//						Log.i("MULTIMEDIA","video-> "+planet.getMultimediaHappy().getVideo().getVideoTitle());
						
						Intent toMultimedia = new Intent(PlanetDetailActivity.this, VideoPlayerActivity.class);
						System.gc();
			        	finish();
			        	startActivity(toMultimedia);
					}
					
				}else{
					
					// parte SAD
					if(!planet.getMultimediaSad().getAudio().getAudioTitle().equals("")){
						// contenuto audio
//						Log.i("MULTIMEDIA","audio-> "+planet.getMultimediaSad().getAudio().getAudioTitle());
						
						Intent toMultimedia = new Intent(PlanetDetailActivity.this, AudioPlayerActivity.class);
						System.gc();
			        	finish();
			        	startActivity(toMultimedia);
						
					}else{
						// contenuto video
//						Log.i("MULTIMEDIA","video-> "+planet.getMultimediaSad().getVideo().getVideoTitle());
						
						Intent toMultimedia = new Intent(PlanetDetailActivity.this, VideoPlayerActivity.class);
						System.gc();
			        	finish();
			        	startActivity(toMultimedia);
					}
					
				}
				
				
	
			}
		});
		
		imageButton = (ImageButton) findViewById(R.id.leftButton);
		imageButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				Intent toImageSet = new Intent(PlanetDetailActivity.this, ScreenSlideActivity.class);

				System.gc();
	        	finish();
				startActivity(toImageSet);
			}
		});
		
		warperButton = (ImageButton) findViewById(R.id.warpToUniverseBtn);
		// controlla che esita una connessione esterna, se non esiste nascondi tutto
		if(planet.getExtraConnections().size()>0){
		warperButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Toast.makeText(
						getApplicationContext(),
						"Ti sto portando in un altro universo...", Toast.LENGTH_LONG).show();
				
				
				Intent toPlanet = new Intent(PlanetDetailActivity.this, PlanetDetailActivity.class);
				SingletonSupport.getInstance().setUniverseID(planet.getExtraConnections().get(0).getIdUniverse());
				SingletonSupport.getInstance().setPlanetID(planet.getExtraConnections().get(0).getPlanetId());

				System.gc();
	        	finish();
				startActivity(toPlanet);
			}
		});
		} else {
			warperButton.setVisibility(View.GONE);
		}
			
	}

	public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuItem item = menu.add(Menu.NONE, R.id.action_flip, Menu.NONE, R.string.action_back);
        item.setIcon( R.drawable.ic_action_info);
        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        return true;
    }
	
	public boolean onOptionsItemSelected(MenuItem item) {
		
		if(item.getItemId() == R.id.action_flip){
			
	        	Intent toInfo = new Intent(PlanetDetailActivity.this, InfoActivity.class);
	        	toInfo.putExtra("TITLE", "Informazioni sul pianeta");
	        	toInfo.putExtra("TEXT", planet.getDescription());
//				toPlanet.putExtra("PLANET_ID", ps.getIdPlanet());
				startActivity(toInfo);
	        
        }
        return super.onOptionsItemSelected(item);
    }
	
	@Override
	public void onBackPressed() {
		Intent toUniverse = new Intent(PlanetDetailActivity.this,
				GalaxyActivity.class);
		// TODO: settare qui l'ultimo pianeta visitato
		SingletonSupport.getInstance().setLastPlanetVisitedID(SingletonSupport.getInstance().findRealPlanetId(planetId));
		System.gc();
		finish();
		startActivity(toUniverse);
	}

}
